let redis = require('redis');

const options = {
    url: 'redis://39.104.93.152:8379',
    password: '123456'

}

const redisClient = redis.createClient(options)

const listenForMessages = async () => {
    console.log('accept message!')
    const {commandOptions} = redis;
    const lsit = await redisClient.lRange('redis',0,7)
    console.log(lsit)
    // const bl = await redisClient.blPop(commandOptions({isolated:true}),'redis', 0);
    // consgitole.log(bl)
    listenForMessages()

}

const publishMessage = async (message, value) => {
    console.log('publish message')
    const res = await redisClient.lPush('redis', value);
    console.log(res)
}
// 连接成功时的回调
redisClient.on('connect', () => {
    console.log('Connected to Redis server');
    let index = 0;

    setInterval(() => {
        index++
        console.log('create message!', index);
        const msg = { id: index, msg: 'message comming!' + index }
        publishMessage(`redis`, JSON.stringify(msg))
    }, 6000)
    listenForMessages()
});


// 连接错误时的回调
redisClient.on('error', (error) => {
    console.error('Failed to connect to Redis server:', error);
});
redisClient.connect();



